package com.hairline.hairlinemarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.hairline.hairlinemarket.bean.MarketCoupon;
import com.hairline.hairlinemarket.bean.MarketCouponExample;
import com.hairline.hairlinemarket.bean.MarketCouponUser;
import com.hairline.hairlinemarket.bean.MarketCouponUserExample;
import com.hairline.hairlinemarket.mapper.MarketCouponMapper;
import com.hairline.hairlinemarket.mapper.MarketCouponUserMapper;
import com.hairline.hairlinemarket.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:杨小凯
 * Date:2023-04-20 18:46
 * Description:<描述>
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private MarketCouponMapper couponMapper;
    @Autowired
    private MarketCouponUserMapper couponUserMapper;
    /*
    未登录用户查询三条
     */
    @Override
    public List queryList(Integer start, Integer end) {
        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = couponExample.createCriteria();
        //end = end-1;
        //criteria.andLimitBetween(start.shortValue(), end.shortValue());
        //PageHelper.startPage(start+1, end);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);
        ArrayList<MarketCoupon> coupons = new ArrayList<>();
        for (int i = start; i < end; i++) {
            coupons.add(marketCoupons.get(i));
        }
        return coupons;
    }

    /*
    登录用户查询未领取的优惠券三条
     */
    @Override
    public List queryAvailableList(Integer userId, Integer start, Integer end) {
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria couponUserExampleCriteria = couponUserExample.createCriteria();
        couponUserExampleCriteria.andUserIdEqualTo(userId);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(couponUserExample);
        List couponIds=new ArrayList<>();
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            Integer couponId = marketCouponUser.getCouponId();
            couponIds.add(couponId.shortValue());
        }

        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria couponExampleCriteria = couponExample.createCriteria();
        couponExampleCriteria.andIdNotIn(couponIds);
        PageHelper.startPage(start+1, end);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);
        ArrayList<MarketCoupon> coupons = new ArrayList<>();
        for (int i = start; i < end; i++) {
            coupons.add(marketCoupons.get(i));
        }
        return coupons;
    }

    @Override
    public List<MarketCoupon> selectCoupon(Integer userId) {
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria couponUserExampleCriteria = couponUserExample.createCriteria();
        couponUserExampleCriteria.andUserIdEqualTo(userId);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(couponUserExample);

        if (marketCouponUsers!=null&&marketCouponUsers.size()>0){
            ArrayList<Integer> list = new ArrayList<>();
            for (MarketCouponUser marketCouponUser : marketCouponUsers) {
                list.add(marketCouponUser.getCouponId());
            }
            MarketCouponExample couponExample = new MarketCouponExample();
            couponExample.createCriteria().andIdIn(list);
            List<MarketCoupon> couponList = couponMapper.selectByExample(couponExample);
            return couponList;
        }
        return null;
    }
}
